package com.choosefine.it.project.service.Impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.BaseService;
import com.choosefine.it.common.json.JsonResult;
import com.choosefine.it.common.json.JsonUtil;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.choosefine.it.project.dao.ProjectCapitalMapper;
import com.choosefine.it.project.model.dto.ProjectTeamCapitalDetail;
import com.choosefine.it.project.model.dto.SelectProjectCondition;
import com.choosefine.it.project.model.dto.SelectProjectTeam;
import com.choosefine.it.project.model.vo.ProjectTeamCapitalDetailVO;
import com.choosefine.it.project.model.vo.ProjectTeamCapitalVO;
import com.choosefine.it.project.model.vo.ProjectTeamTotalVO;
import com.choosefine.it.project.service.ProjectCapitalService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * Created by yeliang on 2016/12/16.
 */
@Service
public class ProjectCapitalServiceImpl extends BaseService implements ProjectCapitalService{

    @Autowired
    ProjectCapitalMapper projectCapitalMapper;

    @Autowired
    CustomRestTemplate restTemplate;
    
    @Value("#{yaml.get('teamCapital.count')}")
	final String countURL = null;

    /**
     * 获取项目资金汇总
     * @param selectProjectCondition
     * @return
     */
    @Override
    public Page<ProjectTeamCapitalVO> getProjectCapital(SelectProjectCondition selectProjectCondition) {

        if (selectProjectCondition.getPageNum() == null ||selectProjectCondition.getPageSize() == null){
            PageHelper.startPage(1,10);
        }else{
            PageHelper.startPage(selectProjectCondition.getPageNum(),selectProjectCondition.getPageSize());
        }
        Page<ProjectTeamCapitalVO> pageList = projectCapitalMapper.getProjectCapital(selectProjectCondition);
        for (ProjectTeamCapitalVO projectCapitalVO : pageList ){
            if (projectCapitalVO.getProjectCode() != null){
                projectCapitalVO.setTeamCapitalCount(projectCapitalMapper.getProjectTeamCapitalSum(projectCapitalVO.getProjectCode()));
                projectCapitalVO.setMaterialCapitalCount(getCapitalByProjectCodeAndType(projectCapitalVO.getProjectCode(),1));
                projectCapitalVO.setEquipmentCapitalCount(getCapitalByProjectCodeAndType(projectCapitalVO.getProjectCode(),3));
                projectCapitalVO.setFacilitiesCapitalCount(getCapitalByProjectCodeAndType(projectCapitalVO.getProjectCode(),2));
            }
        }
        return pageList;
    }

    @Override
    public Page<ProjectTeamTotalVO> getProjectTeamCapital(SelectProjectTeam selectProjectTeam) {

        PageHelper.startPage(selectProjectTeam.getPageNum(),selectProjectTeam.getPageSize());
        Page<ProjectTeamTotalVO> pageList = projectCapitalMapper.getProjectTeamCapital(selectProjectTeam);
        for(int i=0;i<pageList.size();i++){
        	Map<String,Object>map = new HashMap<>();
        	map.put("projectCode", pageList.get(i).getProjectCode());
        	map.put("teamId", pageList.get(i).getTeamId());
//        	pageList.get(i).setPaid(projectCapitalMapper.getAcountSum(map)==null?new BigDecimal(0):projectCapitalMapper.getAcountSum(map));
//        	pageList.get(i).setActualAmount(pageList.get(i).getActualAmount()==null? new BigDecimal(0):pageList.get(i).getActualAmount());
        	//TODO 计算已付金额
        	pageList.get(i).setPaid(new BigDecimal(0));
        	pageList.get(i).setUnpaid(pageList.get(i).getTotalPrice().subtract(pageList.get(i).getPaid()));
        }
        return pageList;
    }

    @Override
    public Page<ProjectTeamCapitalDetailVO> getProjectTeamCapitalDetail(ProjectTeamCapitalDetail projectTeamCapitalDetail) {

        PageHelper.startPage(projectTeamCapitalDetail.getPageNum(),projectTeamCapitalDetail.getPageSize());
        Page<ProjectTeamCapitalDetailVO> pageList = projectCapitalMapper.getProjectTeamCapitalDetail(projectTeamCapitalDetail);
        return pageList;
    }

    /**
     * 分步处理材料设备设施资金统计
     * @param projectCode
     * @param merchandiseType
     * @return
     */
    public BigDecimal getCapitalByProjectCodeAndType(String projectCode,Integer merchandiseType){

        if(projectCode != null && merchandiseType != null){
        	StringBuffer sb = URLHandlerUtils.getUrlBuffer(shoppingURL, countURL);
        	Map<String, Object> map = new LinkedHashMap<>();
    	    map.put("projectCode", projectCode);
    		map.put("merchandiseType", merchandiseType);
    		String url = URLHandlerUtils.urlFormat(sb, map);
    		String result = restTemplate.getForObject(url, String.class);
//            String CapitalUrl = PropertyUtils.getProperty("shop.project.team.capital.count")
//                    +"?projectCode="+projectCode+"&merchandiseType="+merchandiseType;
//
//            ResponseEntity<String> responseEntity = restTemplate.exchange(CapitalUrl,
//                    HttpMethod.GET, null, String.class);
//            String resultString = responseEntity.getBody().toString();
            JsonResult jsonResult = JsonUtil.toObject(result,JsonResult.class);
            if (jsonResult.getData() == null){
                return new BigDecimal(0.00);
            }
            Double data = Double.parseDouble(jsonResult.getData().toString());
            return new BigDecimal(data);
        }

        return new BigDecimal(0.0);
    }
}
